package com.huishu.goods.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.pagination.optimize.JsqlParserCountOptimize;
import com.baomidou.mybatisplus.extension.spring.MybatisSqlSessionFactoryBean;
import io.seata.rm.datasource.DataSourceProxy;
import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;

/**
 * @ClassName：MybatisPlusConfig
 * @Author: ljj
 * @Date: 2022/6/30 9:09
 */
@Configuration
@MapperScan("com.huishu.goods.mapper*")
@EnableTransactionManagement
public class MybatisPlusConfig {
    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.druid")
    public DataSource druidDataSource() {
        return new DruidDataSource();
    }

    /**
     * 构造datasource代理对象，替换原来的datasource
     *
     * @param druidDataSource
     * @return
     */
    @Primary
    @Bean("dataSource")
    public DataSourceProxy dataSourceProxy(DataSource druidDataSource) {
        return new DataSourceProxy(druidDataSource);
    }

    //@Bean(name = "sqlSessionFactory")
    //public SqlSessionFactory sqlSessionFactoryBean(DataSourceProxy dataSourceProxy) throws Exception {
    //    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    //    // 设置代理数据源
    //    factoryBean.setDataSource(dataSourceProxy);
    //    Interceptor[] plugins = new Interceptor[1];
    //    plugins[0] = paginationInterceptor();
    //    factoryBean.setPlugins(plugins);
    //    // ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    //    // factoryBean.setMapperLocations(resolver.getResources("classpath*:mybatis/**/*-mapper.xml"));
//
    //    org.apache.ibatis.session.Configuration configuration=new org.apache.ibatis.session.Configuration();
    //    //使用jdbc的getGeneratedKeys获取数据库自增主键值
    //    configuration.setUseGeneratedKeys(true);
    //    //使用列别名替换列名
    //    configuration.setUseColumnLabel(true);
    //    //自动使用驼峰命名属性映射字段，如userId ---> user_id
    //    configuration.setMapUnderscoreToCamelCase(true);
    //    factoryBean.setConfiguration(configuration);
//
    //    return factoryBean.getObject();
    //}

    @Bean
    public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
        MybatisSqlSessionFactoryBean sqlSessionFactoryBean = new MybatisSqlSessionFactoryBean();
        sqlSessionFactoryBean.setDataSource(dataSource);
        Interceptor[] plugins = new Interceptor[1];
        plugins[0] = paginationInterceptor();
        sqlSessionFactoryBean.setPlugins(plugins);
        //添加XML目录
        // VFS.addImplClass(SpringBootVFS.class);
        // PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // sqlSessionFactoryBean.setMapperLocations(resolver.getResource("classpath:mapping/*.xml"));
        return sqlSessionFactoryBean.getObject();
    }


    /**
     * 新版
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        return mybatisPlusInterceptor;
    }

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        paginationInterceptor.setCountSqlParser(new JsqlParserCountOptimize(true));
        return paginationInterceptor;
    }

}
